import csv
import json
import json
import matplotlib.colors as mcolors
import math
import matplotlib.pyplot as plt
import random
import numpy as np
import pandas as pd
colors = list(dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS).keys())

# plt.figure(figsize=(40, 40), dpi=60)
# ax = plt.subplot(111)

# ax.xaxis.grid(True, which='major') #x坐标轴的网格使用主刻度
# ax.yaxis.grid(True, which='major') #x坐标轴的网格使用主刻度

def show_path_by_id():
    pp=[4483,15083]#[15238, 15230, 29, 15114, 5056, 5058, 4483, 14999] #[15404, 15405, 730, 731, 732, 733, 734, 446, 445, 444, 443, 442, 441, 440, 439, 438, 437, 436, 435, 434, 433, 432, 431, 430, 429, 428, 427, 426, 425, 424, 423, 422, 421, 420, 419, 418, 417, 416, 415, 414, 413, 412, 411, 410, 409, 408, 407, 406, 405, 404, 403, 402, 401, 14456, 15062, 6067, 15060, 15059, 15058, 15057, 15221, 15220, 15511, 15510, 15509, 15212, 15507, 15506, 15505, 15504, 11259, 15502, 15501, 15500, 921, 15498, 15497, 15496, 15495, 15494, 15493, 15492, 15114]
    points, roadLines, roadpoint = ng31. view_maps2(True)  # 读取地图
    d3=[]
    for id in range(len(pp)):
        d3.append(points[pp[id]])
    d4 = np.array(d3)
    plt.plot(d4[:, 1], d4[:, 0], color=colors[0],
                marker='.')

    plt.show()
def view_map(index):
    with open("D:/session/map/RoadLine.json", 'rb') as load_f:
        RoadLine = json.load(load_f)
    dt = RoadLine[index]["geometry"]["coordinates"]
    if dt == []:
        return
    dtarray = np.array(dt)
    plt.plot(dtarray[:, 1], dtarray[:, 0], color=colors[0], marker='.')

    # with open("./data/RoadPoint.json", 'rb') as load_f:
    #     RoadPoint = json.load(load_f)

    # for rd in RoadPoint:  # len(RoadLine)):
    #     dt = rd["geometry"]["coordinates"]
    #     if dt == []:
    #         continue
    #     dtarray = np.array(dt)
    #     plt.scatter(dtarray[1], dtarray[0],
    #                 color=colors[1], edgecolors='none', s=100)


def GET_my_path(points):
    # C:/Users/tang/Desktop/RHZT/R1/jsontest3/out.csv
    path = 'C:\\Users\\tang\\Desktop\\cmd\\cpp_app\\gen_people_25.csv'
    read_csv = csv.reader(
        open(path, 'r'), dialect='excel')

    _ldd = 0
    pltlins = []
    D5 = []
    for line in read_csv:  # 循环输出csv中的所有数据
        aint = list(int(val) for val in line)
        d3 = []
        for id in range(len(aint)-1):
            d3.append(points[aint[id]])
        # print(d3)
        d4 = np.array(d3)

        # line1 = ax.
        # ax.legend(line1, (str(_ldd),)) # 多加一个逗号
        # line2 = ax.plot(d4[:,1], d4[:,0], color=colors[_ldd+4], marker='*')
        # ax.legend(line2, (str(_ldd),)) # 多加一个逗号
        plt.plot(d4[:, 1], d4[:, 0], color=colors[_ldd+2],
                 marker='.', label=str(_ldd))

        # print(d4[:,1][0],d4[:,0][0])
        plt.scatter(d4[:, 1][0], d4[:, 0][0], c='r', marker='o', s=700)  # 终点
        plt.scatter(d4[:, 1][len(d3)-1], d4[:, 0][len(d3)-1],
                    c='b', marker='o', s=700)  # 起点
        _ldd += 1
        D5.append(d4)
    # print(D5)


def view_my_path(points, path):
    # path='C:\\Users\\tang\\Desktop\\cmd\\cpp_app\\data\\gen_people_25_new.csv' # C:/Users/tang/Desktop/RHZT/R1/jsontest3/out.csv
    # path='C:\\Users\\tang\\Desktop\\cmd\\data\\gen_people_0.csv'
    read_csv = csv.reader(
        open(path, 'r'), dialect='excel')

    _ldd = 0
    pltlins = []
    for line in read_csv:  # 循环输出csv中的所有数据
        aint = list(int(val) for val in line)
        d3 = []
        for id in range(len(aint)):
            d3.append(points[aint[id]])
        # print(d3)
        d4 = np.array(d3)

        # line1 = ax.
        # ax.legend(line1, (str(_ldd),)) # 多加一个逗号
        # line2 = ax.plot(d4[:,1], d4[:,0], color=colors[_ldd+4], marker='*')
        # ax.legend(line2, (str(_ldd),)) # 多加一个逗号
        plt.plot(d4[:, 1], d4[:, 0], color=colors[_ldd+2],
                 marker='.', label=str(_ldd))

        # print(d4[:,1][0],d4[:,0][0])
        plt.scatter(d4[:, 1][0], d4[:, 0][0],
                    color=colors[_ldd+2], marker='o', s=400)  # 终点
        plt.scatter(d4[:, 1][len(d3)-1], d4[:, 0][len(d3)-1],
                    color='b', marker='o', s=200)  # 起点
        _ldd += 1
        # print(d4)

        # plt.scatter(d4[:,1][0], d4[:,0][0],
        #             color=colors[7], edgecolors='none',marker='*')


def view_json_path():
    with open("./data/data2cpp.json", 'rb') as load_f:
        RoadLine = json.load(load_f)


def view_my_path():
    colors = list(dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS).keys())

    points = view_maps(True)
    # print(len(points))
    view_my_path(
        points, 'C:\\Users\\tang\\Desktop\\cmd\\data\\gen_people_0.csv')
    # view_my_path(points,'C:\\Users\\tang\\Desktop\\cmd\\data\\gen_people_1.csv')
    # view_my_path(points,'C:\\Users\\tang\\Desktop\\cmd\\data\\gen_people_2.csv')

    plt.legend()
    plt.show()

def view_maps(view):
    points = []
    with open("D:/session/map/RoadLine.json", 'rb') as load_f:
        RoadLine = json.load(load_f)
    for rd in RoadLine:  # len(RoadLine)):
        dt = rd["geometry"]["coordinates"]
        if dt == []:
            continue
        points += dt
        if view:
            DTS = np.array(dt)
            plt.plot(DTS[:, 0], DTS[:, 1], color=colors[1], linewidth=0.50)

    with open("D:/session/map/RoadPoint.json", 'rb') as load_f:
        RoadPoint = json.load(load_f)

    for rd in RoadPoint:  # len(RoadLine)):
        dt = rd["geometry"]["coordinates"]
        if dt == []:
            continue
        if view:
            dtarray = np.array(dt)
            plt.scatter(dtarray[0], dtarray[1],
                        color=colors[0], edgecolors='none', s=10, linewidth=0.50)
    return points,DTS,dtarray
def view_ai_path():
    # points,DTS,dtarray = view_maps(True)
    # ponp=np.array(points)
    # with open("C:\\Users\\Administrator\\Desktop\\Test_Exam\\session\\session_3\\subject_2\\team_path_1.json", 'rb') as load_f:

    with open("C:\\Users\\tang\\Desktop\\RHZT\\Test_Exam\\session\\session_3\\subject_2\\team_path_1.json", 'rb') as load_f:
        RoadLine = json.load(load_f)
    points,DTS,dtarray = view_maps(True)

    for peo in RoadLine["firstPlaces"][1]["people"]:
 
        if(peo["people_id"]!=200):
            continue
        paths=[]
        
        for id in peo["pass_points"]:
            paths.append([id["longitude"],id["latitude"]])
        d4 = np.array(paths)
        print(paths)
        plt.plot(d4[:, 0], d4[:, 1], color=colors[0],
                    marker='.')
    for peo in RoadLine["secondPlaces"][1]["people"]:
 
        if(peo["people_id"]!=200):
            continue
        paths=[]
        
        for id in peo["pass_points"]:
            paths.append([id["longitude"],id["latitude"]])
        d4 = np.array(paths)
        print(paths)
        plt.plot(d4[:, 0], d4[:, 1], color=colors[1],
                    marker='.')

        # plt.plot(ponp[:, 1], ponp[:, 0], color=colors[1], linewidth=0.50)
            


        # plt.savefig("./p/"+str(RoadLine["places"][0]["people"][i]["people_id"])+".png")
        #plt.clf()
    plt.show()
# if __name__ == '__main__':
#     points,DTS,dtarray = view_maps(True)
#     print(len(points))
#     plt.show()
    # D:\session\session_3\subject_1
view_ai_path()